package com.haixiaoke.saas.repository.util;


import lombok.Data;

import java.util.Map;

/**
 * 订阅消息模板对象
 */
@Data
public class Template {

    private String toUser;
    private String templateId;
    private String page;
    private Map<String,Object> data;

    public String toJSON() {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        builder.append(String.format("\"touser\":\"%s\"", this.toUser)).append(",");
        builder.append(String.format("\"template_id\":\"%s\"", this.templateId)).append(",");
        builder.append(String.format("\"page\":\"%s\"", this.page)).append(",");
        builder.append("\"data\":{");

        for (String key : data.keySet()) {
            // 判断是否追加逗号
            builder.append(String.format("\"%s\": {\"value\":\"%s\"},", key, data.get(key)));
        }
        builder.deleteCharAt(builder.lastIndexOf(","));
        builder.append("}");
        builder.append("}");
        return builder.toString();
    }


}
